// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/type/hive/hive_type_serializer.h>

#include <cstddef>
#include <stdexcept>

using kumo::pollux::Type;
using kumo::pollux::TypeKind;

namespace kumo::pollux::type::hive {
    std::string HiveTypeSerializer::serialize(
        const std::shared_ptr<const Type> &type) {
        HiveTypeSerializer serializer;
        return serializer.visit(*type);
    }

    std::string HiveTypeSerializer::visit(const Type &type) const {
        switch (type.kind()) {
            case TypeKind::BOOLEAN:
                return "boolean";
            case TypeKind::TINYINT:
                return "tinyint";
            case TypeKind::SMALLINT:
                return "smallint";
            case TypeKind::INTEGER:
                return "int";
            case TypeKind::BIGINT:
                return "bigint";
            case TypeKind::REAL:
                return "float";
            case TypeKind::DOUBLE:
                return "double";
            case TypeKind::VARCHAR:
                return "string";
            case TypeKind::VARBINARY:
                return "binary";
            case TypeKind::TIMESTAMP:
                return "timestamp";
            case TypeKind::ARRAY:
                return "array<" + visitChildren(type.as_array()) + ">";
            case TypeKind::MAP:
                return "map<" + visitChildren(type.as_map()) + ">";
            case TypeKind::ROW:
                return "struct<" + visitChildren(type.as_row()) + ">";
            case TypeKind::OPAQUE: {
                auto typeAlias = getOpaqueAliasForTypeId(type.as_opaque().typeIndex());
                return "opaque<" + typeAlias + ">";
            }
            default:
                POLLUX_UNSUPPORTED("unsupported type: " + type.toString());
        }
    }

    std::string HiveTypeSerializer::visitChildren(const pollux::RowType &t) const {
        std::string result;
        for (size_t i = 0; i < t.size(); ++i) {
            if (i != 0) {
                result += ",";
            }
            result += t.nameOf(i);
            result += ":";
            result += visit(*t.childAt(i));
        }

        return result;
    }

    std::string HiveTypeSerializer::visitChildren(const pollux::Type &t) const {
        std::string result;
        for (size_t i = 0; i < t.size(); ++i) {
            if (i != 0) {
                result += ",";
            }
            result += visit(*t.childAt(i));
        }

        return result;
    }
} // namespace kumo::pollux::type::hive
